package com.bosssoft.service.impl;

import com.bosssoft.entity.dto.FibAccountDTO;
import com.bosssoft.entity.po.FibVoucherDetailsPO;
import com.bosssoft.entity.po.FibVoucherPO;
import com.bosssoft.enums.EleSerCode;
import com.bosssoft.excelEntity.ExcelConvertPDF;
import com.bosssoft.excelEntity.PO.*;
import com.bosssoft.excelEntity.VO.*;
import com.bosssoft.exception.DaoException;
import com.bosssoft.fegin.AccountFeginService;
import com.bosssoft.fegin.OssFeginService;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibCreateVoucherService;
import com.bosssoft.service.FibVoucherDetailsService;
import com.bosssoft.service.FibVoucherService;
import com.bosssoft.util.MoneyChangeUtil;
import com.itextpdf.text.RectangleReadOnly;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author Aria
 */
@Slf4j
@Service
public class FibCreateVoucherServiceImpl implements FibCreateVoucherService {

    //静态excel模板地址
    private static final String resourcesDir = "fiscal-integration/fib-electronic-server/src/main/resources/excel";
    private static final String sysName = "system";
    private static final String financial = "60000000600000001";
    //
    private static final Integer directPaymentSummaryClearingLineNoticeGist = 50181;
    private static final Integer directPaymentSummaryClearingLineNotice = 50180;
    private static final Integer directPfinancialPayment = 52011;
    private static final Integer financialDirectPaymentEntryNotice = 22050;
    private static final Integer payRefundNoticeDirectlyCode = 22031;
    private static final Integer voucherBudgetAllocation = 52070;
    private static final Integer voucherBudgetAllocationReceipt = 52071;

    @Autowired
    private FibVoucherService fibVoucherService;

    @Autowired
    private FibVoucherDetailsService fibVoucherDetailsService;

    @Autowired
    private AccountFeginService accountFeginService;

    @Autowired
    private OssFeginService ossFeginService;

    /**
     * 根据vo生成财政直接支付凭证
     */
    @Override
    public FibVoucherDetailsPO createDirectPfinancialPayment(DirectPfinancialPaymentVO directPfinancialPaymentVO) throws Exception {

        BigDecimal moneySmall;
        //支付人账户
        FibAccountDTO refundAccount;
        //收款人账户
        FibAccountDTO payAccount;
        //转换填充参数
        DirectPfinancialPaymentPO directPfinancialPaymentPO = new DirectPfinancialPaymentPO();
        BeanUtils.copyProperties(directPfinancialPaymentVO, directPfinancialPaymentPO);
        //参数检查
        try {
            moneySmall = new BigDecimal(directPfinancialPaymentVO.getMoneySmall());
        } catch (DaoException e) {
            e.printStackTrace();
            log.error("金额转换有误");
            throw DaoException.error(EleSerCode.MONEY_CHANGE_ERROR);
        }
        //判断账户是否存在
        Result payer = accountFeginService.select(Long.valueOf(directPfinancialPaymentVO.getPayerAccount()));
        Result payee = accountFeginService.select(Long.valueOf(directPfinancialPaymentVO.getPayeeAccount()));
        if (payer.getData().get("list") == null || payee.getData().get("list") == null) {
            log.error("账户不存在");
            throw DaoException.error(EleSerCode.BANK_IS_NOT_FOUND);
        }
        //TODO 判断用户是否存在
        //得到大写的金额
        String moneyBig = MoneyChangeUtil.toChinese(String.valueOf(moneySmall));
        directPfinancialPaymentPO.setMoneyBig(moneyBig);
        //创建时间
        String timeStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        directPfinancialPaymentPO.setCreatTime(timeStr);
        //对模板操作
        FibVoucherPO fibVoucherPO = fibVoucherService.getBaseMapper().selectById(directPfinancialPayment);
        //文件名加后缀
        String voucherName = fibVoucherPO.getVoucherName() + ".pdf";
        //模板在本地的地址
        String voucherPath = fibVoucherPO.getVoucherItems().trim();
        //设置模板地址
        String templateUrl2 = resourcesDir + "/" + voucherPath;
        //设置页数
        Map<Object, String> voAndSheetName2 = new HashMap<>();
        voAndSheetName2.put(directPfinancialPaymentPO, "Sheet1");
        //填充属性操作
        //将模版和要写入模版的值传入，转换成workbook
        Workbook workbook = ExcelConvertPDF.outPutWorkbookByModel(voAndSheetName2, templateUrl2);
        List<Workbook> workbooks = new ArrayList<>();
        workbooks.add(workbook);
        //设置导出的页面的大小
        RectangleReadOnly pageSize = new RectangleReadOnly(1000.0F, 400.0F);
        //获取转换成pdf的字节流
        byte[] bytes = ExcelConvertPDF.ExcelConvertPDFMultipartFile(workbooks, pageSize);
        //转换成 输入流
        InputStream is = new ByteArrayInputStream(bytes);
        //封装进MockMultipartFile
        MultipartFile multipartFile = new MockMultipartFile(voucherName, voucherName, "application/pdf", is);
        //图片上传
        Result upload = ossFeginService.upload(multipartFile);
        //云端图片地址
        //-------封装voucherDetails记录----------------
        //创建数据库记录
        FibVoucherDetailsPO fibVoucherDetailsPO = new FibVoucherDetailsPO();
        //图片地址
        fibVoucherDetailsPO.setVoucherUrlNotSeal((String) upload.getData().get("url"));
        //凭证类型编号
        fibVoucherDetailsPO.setVoucherCode(directPfinancialPayment);
        //创建人,更新人
        fibVoucherDetailsPO.setCreateBy(sysName);
        //预算申请表编号
        fibVoucherDetailsPO.setBgtId(directPfinancialPaymentVO.getBgtId());
        //金额
        fibVoucherDetailsPO.setVoucherAmount(new BigDecimal(directPfinancialPaymentVO.getMoneySmall()));
        //付款人,收款人编号
        fibVoucherDetailsPO.setVoucherPayUserid(Long.valueOf(directPfinancialPaymentVO.getPayerId()));
        fibVoucherDetailsPO.setVoucherAccountUserid(Long.valueOf(directPfinancialPaymentVO.getPayeeId()));
        //状态 未盖章
        fibVoucherDetailsPO.setStatus(0);
        fibVoucherDetailsPO.setCreateTime(new Date());
        fibVoucherDetailsPO.setVersion(1);
        fibVoucherDetailsPO.setDeleted((byte) 1);
        fibVoucherDetailsService.getBaseMapper().insert(fibVoucherDetailsPO);
        is.close();
        return fibVoucherDetailsPO;
    }

    @Override
    public FibVoucherDetailsPO createFinancialDirectPaymentEntryNotice(FinancialDirectPaymentEntryNoticeVO financialDirectPaymentEntryNoticeVO) throws Exception {
        //转换填充参数
        FinancialDirectPaymentEntryNoticePO financialDirectPaymentEntryNoticePO = new FinancialDirectPaymentEntryNoticePO();
        BeanUtils.copyProperties(financialDirectPaymentEntryNoticeVO, financialDirectPaymentEntryNoticePO);
        //对po进行填充
        String timeStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        financialDirectPaymentEntryNoticePO.setTime(timeStr);
        String combinedLowercase = financialDirectPaymentEntryNoticeVO.getCombinedLowercase();
        try {
            new BigDecimal(combinedLowercase);
        } catch (DaoException e) {
            e.printStackTrace();
            log.error("金额转换有误");
            throw DaoException.error(EleSerCode.MONEY_CHANGE_ERROR);
        }
        //转换成大写
        financialDirectPaymentEntryNoticePO.setCombinedBig(MoneyChangeUtil.toChinese(combinedLowercase));
        //对模板操作
        //读取模板
        FibVoucherPO fibVoucherPO = fibVoucherService.getBaseMapper().selectById(financialDirectPaymentEntryNotice);
        //文件名加后缀
        String voucherName = fibVoucherPO.getVoucherName() + ".pdf";
        //模板在本地的地址
        String voucherPath = fibVoucherPO.getVoucherItems().trim();
        //设置模板地址
        String templateUrl2 = resourcesDir + "/" + voucherPath;
        //设置页数
        Map<Object, String> voAndSheetName2 = new HashMap<>();
        voAndSheetName2.put(financialDirectPaymentEntryNoticePO, "Sheet1");
        //填充属性操作
        //将模版和要写入模版的值传入，转换成workbook
        Workbook workbook = ExcelConvertPDF.outPutWorkbookByModel(voAndSheetName2, templateUrl2);
        List<Workbook> workbooks = new ArrayList<>();
        workbooks.add(workbook);
        //设置导出的页面的大小
        RectangleReadOnly pageSize = new RectangleReadOnly(1000.0F, 400.0F);
        //获取转换成pdf的字节流
        byte[] bytes = ExcelConvertPDF.ExcelConvertPDFMultipartFile(workbooks, pageSize);
        //转换成 输入流
        InputStream is = new ByteArrayInputStream(bytes);
        //封装进MockMultipartFile
        MultipartFile multipartFile = new MockMultipartFile(voucherName, voucherName, "application/pdf", is);
        //图片上传
        Result upload = ossFeginService.upload(multipartFile);
        //云端图片地址
        //-------封装voucherDetails记录----------------
        //创建数据库记录
        FibVoucherDetailsPO fibVoucherDetailsPO = new FibVoucherDetailsPO();
        //图片地址
        fibVoucherDetailsPO.setVoucherUrlNotSeal((String) upload.getData().get("url"));

        fibVoucherDetailsPO.setVoucherCode(financialDirectPaymentEntryNotice);
        //创建人,更新人
        fibVoucherDetailsPO.setCreateBy(sysName);
        //预算申请表编号
        fibVoucherDetailsPO.setBgtId(financialDirectPaymentEntryNoticeVO.getBgtId());
        //金额
        fibVoucherDetailsPO.setVoucherAmount(new BigDecimal(financialDirectPaymentEntryNoticeVO.getCombinedLowercase()));
        //付款人,收款人编号
        fibVoucherDetailsPO.setVoucherPayUserid(Long.valueOf(financial));
        fibVoucherDetailsPO.setVoucherAccountUserid(Long.valueOf(financialDirectPaymentEntryNoticeVO.getAccount()));
        //状态 未盖章
        fibVoucherDetailsPO.setStatus(0);
        fibVoucherDetailsPO.setCreateTime(new Date());
        fibVoucherDetailsPO.setVersion(1);
        fibVoucherDetailsPO.setDeleted((byte) 1);
        fibVoucherDetailsService.getBaseMapper().insert(fibVoucherDetailsPO);
        is.close();
        return fibVoucherDetailsPO;
    }

    @Override
    public FibVoucherDetailsPO createDirectPaymentSummaryClearingLineNoticeGist(DirectPaymentSummaryClearingLineNoticeGistVO directPaymentSummaryClearingLineNoticeGistVO) throws Exception {
        //转换填充参数
        DirectPaymentSummaryClearingLineNoticeGistPO directPaymentSummaryClearingLineNoticeGistPO = new DirectPaymentSummaryClearingLineNoticeGistPO();
        BeanUtils.copyProperties(directPaymentSummaryClearingLineNoticeGistVO, directPaymentSummaryClearingLineNoticeGistPO);
        //判断账户是否存在
        Result payer = accountFeginService.select(Long.valueOf(directPaymentSummaryClearingLineNoticeGistVO.getPayerAccount()));
        Result payee = accountFeginService.select(Long.valueOf(directPaymentSummaryClearingLineNoticeGistVO.getPayeeAccount()));
        if (payer.getData().get("list") == null || payee.getData().get("list") == null) {
            log.error("账户不存在");
            throw DaoException.error(EleSerCode.BANK_IS_NOT_FOUND);
        }
        //对po进行填充
        String timeStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        directPaymentSummaryClearingLineNoticeGistPO.setTime(timeStr);
        BigDecimal money = BigDecimal.valueOf(0);
        //计算总共的金额
        String moneyOne = directPaymentSummaryClearingLineNoticeGistPO.getClearingLines();
        String moneyTwo = directPaymentSummaryClearingLineNoticeGistPO.getClearingLinesTwo();
        String moneyThree = directPaymentSummaryClearingLineNoticeGistPO.getClearingLinesThree();
        String moneyFour = directPaymentSummaryClearingLineNoticeGistPO.getClearingLinesFour();
        String moneyFive = directPaymentSummaryClearingLineNoticeGistPO.getClearingLinesFive();
        String moneySix = directPaymentSummaryClearingLineNoticeGistPO.getClearingLinesSix();
        //如果为空设置为零
        moneyOne = moneyOne == null ? moneyOne : "0";
        moneyTwo = moneyTwo == null ? moneyOne : "0";
        moneyThree = moneyThree == null ? moneyThree : "0";
        moneyFour = moneyFour == null ? moneyFour : "0";
        moneyFive = moneyFive == null ? moneyFive : "0";
        moneySix = moneySix == null ? moneySix : "0";
        try {
            BigDecimal bigDecimal1 = new BigDecimal(moneyOne);
            BigDecimal bigDecimal2 = new BigDecimal(moneyTwo);
            BigDecimal bigDecimal3 = new BigDecimal(moneyThree);
            BigDecimal bigDecimal4 = new BigDecimal(moneyFour);
            BigDecimal bigDecimal5 = new BigDecimal(moneyFive);
            BigDecimal bigDecimal6 = new BigDecimal(moneySix);
            money = money.add(bigDecimal1);
            money = money.add(bigDecimal2);
            money = money.add(bigDecimal3);
            money = money.add(bigDecimal4);
            money = money.add(bigDecimal5);
            money = money.add(bigDecimal6);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("数字转换异常");
            throw DaoException.error(EleSerCode.MONEY_CHANGE_ERROR);
        }
        directPaymentSummaryClearingLineNoticeGistPO.setCombined(money.toString());
        //对模板操作
        //读取模板
        FibVoucherPO fibVoucherPO = fibVoucherService.getBaseMapper().selectById(directPaymentSummaryClearingLineNotice);
        //文件名加后缀
        String voucherName = fibVoucherPO.getVoucherName() + ".pdf";
        //模板在本地的地址
        String voucherPath = fibVoucherPO.getVoucherItems().trim();
        //设置模板地址
        String templateUrl2 = resourcesDir + "/" + voucherPath;
        //设置页数
        Map<Object, String> voAndSheetName2 = new HashMap<>();
        voAndSheetName2.put(directPaymentSummaryClearingLineNoticeGistPO, "Sheet1");
        //填充属性操作
        //将模版和要写入模版的值传入，转换成workbook
        Workbook workbook = ExcelConvertPDF.outPutWorkbookByModel(voAndSheetName2, templateUrl2);
        List<Workbook> workbooks = new ArrayList<>();
        workbooks.add(workbook);
        //设置导出的页面的大小
        RectangleReadOnly pageSize = new RectangleReadOnly(1000.0F, 400.0F);
        //获取转换成pdf的字节流
        byte[] bytes = ExcelConvertPDF.ExcelConvertPDFMultipartFile(workbooks, pageSize);
        //转换成 输入流
        InputStream is = new ByteArrayInputStream(bytes);
        //封装进MockMultipartFile
        MultipartFile multipartFile = new MockMultipartFile(voucherName, voucherName, "application/pdf", is);
        //图片上传
        Result upload = ossFeginService.upload(multipartFile);
        //云端图片地址
        //-------封装voucherDetails记录----------------
        //创建数据库记录
        FibVoucherDetailsPO fibVoucherDetailsPO = new FibVoucherDetailsPO();
        //图片地址
        fibVoucherDetailsPO.setVoucherUrlNotSeal((String) upload.getData().get("url"));
        //凭证类型编号
        fibVoucherDetailsPO.setVoucherCode(directPaymentSummaryClearingLineNoticeGist);
        //创建人,更新人
        fibVoucherDetailsPO.setCreateBy(sysName);
        //预算申请表编号
        fibVoucherDetailsPO.setBgtId(directPaymentSummaryClearingLineNoticeGistVO.getBgtId());
        //金额
        fibVoucherDetailsPO.setVoucherAmount(money);
        //付款人,收款人编号
        fibVoucherDetailsPO.setVoucherPayUserid(Long.valueOf(directPaymentSummaryClearingLineNoticeGistVO.getPayerId()));
        fibVoucherDetailsPO.setVoucherAccountUserid(Long.valueOf(directPaymentSummaryClearingLineNoticeGistVO.getPayeeId()));
        //状态 未盖章
        fibVoucherDetailsPO.setStatus(0);
        fibVoucherDetailsPO.setCreateTime(new Date());
        fibVoucherDetailsPO.setVersion(1);
        fibVoucherDetailsPO.setDeleted((byte) 1);
        fibVoucherDetailsService.getBaseMapper().insert(fibVoucherDetailsPO);
        is.close();
        return fibVoucherDetailsPO;
    }

    @Override
    public FibVoucherDetailsPO createDirectPaymentSummaryClearingLineNotice(DirectPaymentSummaryClearingLineNoticeVO directPaymentSummaryClearingLineNoticeVO) throws Exception {
        //转换填充参数
        DirectPaymentSummaryClearingLineNoticePO directPaymentSummaryClearingLineNoticePO = new DirectPaymentSummaryClearingLineNoticePO();
        BeanUtils.copyProperties(directPaymentSummaryClearingLineNoticeVO, directPaymentSummaryClearingLineNoticePO);
        //判断账户是否存在
        Result payer = accountFeginService.select(Long.valueOf(directPaymentSummaryClearingLineNoticeVO.getPayerAccount()));
        Result payee = accountFeginService.select(Long.valueOf(directPaymentSummaryClearingLineNoticeVO.getPayeeAccount()));
        System.out.println(payer.getData().get("list"));
        System.out.println(payee.getData().get("list"));
        if (payer.getData().get("list") == null || payee.getData().get("list") == null) {
            log.error("账户不存在");
            throw DaoException.error(EleSerCode.BANK_IS_NOT_FOUND);
        }
        //对po进行填充
        String timeStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        directPaymentSummaryClearingLineNoticePO.setTime(timeStr);
        BigDecimal money = BigDecimal.valueOf(0);
        //计算总共的金额
        String moneyOne = directPaymentSummaryClearingLineNoticePO.getClearingLines();
        String moneyTwo = directPaymentSummaryClearingLineNoticePO.getClearingLinesTwo();
        String moneyThree = directPaymentSummaryClearingLineNoticePO.getClearingLinesThree();
        String moneyFour = directPaymentSummaryClearingLineNoticePO.getClearingLinesFour();
        String moneyFive = directPaymentSummaryClearingLineNoticePO.getClearingLinesFive();
        String moneySix = directPaymentSummaryClearingLineNoticePO.getClearingLinesSix();
        //如果为空设置为零
        moneyOne = moneyOne == null ? moneyOne : "0";
        moneyTwo = moneyTwo == null ? moneyOne : "0";
        moneyThree = moneyThree == null ? moneyThree : "0";
        moneyFour = moneyFour == null ? moneyFour : "0";
        moneyFive = moneyFive == null ? moneyFive : "0";
        moneySix = moneySix == null ? moneySix : "0";
        try {
            BigDecimal bigDecimal1 = new BigDecimal(moneyOne);
            BigDecimal bigDecimal2 = new BigDecimal(moneyTwo);
            BigDecimal bigDecimal3 = new BigDecimal(moneyThree);
            BigDecimal bigDecimal4 = new BigDecimal(moneyFour);
            BigDecimal bigDecimal5 = new BigDecimal(moneyFive);
            BigDecimal bigDecimal6 = new BigDecimal(moneySix);
            money = money.add(bigDecimal1);
            money = money.add(bigDecimal2);
            money = money.add(bigDecimal3);
            money = money.add(bigDecimal4);
            money = money.add(bigDecimal5);
            money = money.add(bigDecimal6);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("数字转换异常");
            throw DaoException.error(EleSerCode.MONEY_CHANGE_ERROR);
        }
        directPaymentSummaryClearingLineNoticePO.setCombined(money.toString());
        //对模板操作
        //读取模板
        FibVoucherPO fibVoucherPO = fibVoucherService.getBaseMapper().selectById(directPaymentSummaryClearingLineNoticeGist);
        //文件名加后缀
        String voucherName = fibVoucherPO.getVoucherName() + ".pdf";
        //模板在本地的地址
        String voucherPath = fibVoucherPO.getVoucherItems().trim();
        //设置模板地址
        String templateUrl2 = resourcesDir + "/" + voucherPath;
        //设置页数
        Map<Object, String> voAndSheetName2 = new HashMap<>();
        voAndSheetName2.put(directPaymentSummaryClearingLineNoticePO, "Sheet1");
        //填充属性操作
        //将模版和要写入模版的值传入，转换成workbook
        Workbook workbook = ExcelConvertPDF.outPutWorkbookByModel(voAndSheetName2, templateUrl2);
        List<Workbook> workbooks = new ArrayList<>();
        workbooks.add(workbook);
        //设置导出的页面的大小
        RectangleReadOnly pageSize = new RectangleReadOnly(1000.0F, 400.0F);
        //获取转换成pdf的字节流
        byte[] bytes = ExcelConvertPDF.ExcelConvertPDFMultipartFile(workbooks, pageSize);
        //转换成 输入流
        InputStream is = new ByteArrayInputStream(bytes);
        //封装进MockMultipartFile
        MultipartFile multipartFile = new MockMultipartFile(voucherName, voucherName, "application/pdf", is);
        //图片上传
        Result upload = ossFeginService.upload(multipartFile);
        //云端图片地址
        //-------封装voucherDetails记录----------------
        //创建数据库记录
        FibVoucherDetailsPO fibVoucherDetailsPO = new FibVoucherDetailsPO();
        //图片地址
        fibVoucherDetailsPO.setVoucherUrlNotSeal((String) upload.getData().get("url"));
        //凭证类型编号
        fibVoucherDetailsPO.setVoucherCode(directPaymentSummaryClearingLineNoticeGist);
        //创建人,更新人
        fibVoucherDetailsPO.setCreateBy(sysName);
        //预算申请表编号
        fibVoucherDetailsPO.setBgtId(directPaymentSummaryClearingLineNoticeVO.getBgtId());
        //金额
        fibVoucherDetailsPO.setVoucherAmount(money);
        //付款人,收款人编号
        fibVoucherDetailsPO.setVoucherPayUserid(Long.valueOf(directPaymentSummaryClearingLineNoticeVO.getPayerId()));
        fibVoucherDetailsPO.setVoucherAccountUserid(Long.valueOf(directPaymentSummaryClearingLineNoticeVO.getPayeeId()));
        //状态 未盖章
        fibVoucherDetailsPO.setStatus(0);
        fibVoucherDetailsPO.setCreateTime(new Date());
        fibVoucherDetailsPO.setVersion(1);
        fibVoucherDetailsPO.setDeleted((byte) 1);
        fibVoucherDetailsService.getBaseMapper().insert(fibVoucherDetailsPO);
        is.close();
        return fibVoucherDetailsPO;
    }

    @Override
    public FibVoucherDetailsPO createVoucherBudgetAllocation(VoucherBudgetAllocationVO voucherBudgetAllocationVO) throws Exception {
        //转换填充参数
        VoucherBudgetAllocationPO voucherBudgetAllocationPO = new VoucherBudgetAllocationPO();
        BeanUtils.copyProperties(voucherBudgetAllocationVO, voucherBudgetAllocationPO);
        //对po进行填充
        String timeStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        voucherBudgetAllocationPO.setTime(timeStr);
        //小写金额
        String combinedLowercase = voucherBudgetAllocationPO.getAmountMoneyLowercase();
        try {
            new BigDecimal(combinedLowercase);
        } catch (DaoException e) {
            e.printStackTrace();
            log.error("金额转换有误");
            throw DaoException.error(EleSerCode.MONEY_CHANGE_ERROR);
        }
        //转换成大写
        voucherBudgetAllocationPO.setAmountMoneyCapital(MoneyChangeUtil.toChinese(combinedLowercase));
        //对模板操作
        //读取模板
        FibVoucherPO fibVoucherPO = fibVoucherService.getBaseMapper().selectById(voucherBudgetAllocation);
        //FibVoucherPO fibVoucherPO = fibVoucherService.getBaseMapper().selectById(voucherBudgetAllocation);
        //文件名加后缀
        String voucherName = fibVoucherPO.getVoucherName() + ".pdf";
        //模板在本地的地址
        String voucherPath = fibVoucherPO.getVoucherItems().trim();
        //设置模板地址
        String templateUrl2 = resourcesDir + "/" + voucherPath;
        //设置页数
        Map<Object, String> voAndSheetName2 = new HashMap<>();
        voAndSheetName2.put(voucherBudgetAllocationPO, "Sheet1");
        //填充属性操作
        //将模版和要写入模版的值传入，转换成workbook
        Workbook workbook = ExcelConvertPDF.outPutWorkbookByModel(voAndSheetName2, templateUrl2);
        List<Workbook> workbooks = new ArrayList<>();
        workbooks.add(workbook);
        //设置导出的页面的大小
        RectangleReadOnly pageSize = new RectangleReadOnly(1000.0F, 400.0F);
        //获取转换成pdf的字节流
        byte[] bytes = ExcelConvertPDF.ExcelConvertPDFMultipartFile(workbooks, pageSize);
        //转换成 输入流
        InputStream is = new ByteArrayInputStream(bytes);
        //封装进MockMultipartFile
        MultipartFile multipartFile = new MockMultipartFile(voucherName, voucherName, "application/pdf", is);
        //图片上传
        Result upload = ossFeginService.upload(multipartFile);
        //云端图片地址
        //-------封装voucherDetails记录----------------
        //创建数据库记录
        FibVoucherDetailsPO fibVoucherDetailsPO = new FibVoucherDetailsPO();
        //图片地址
        fibVoucherDetailsPO.setVoucherUrlNotSeal((String) upload.getData().get("url"));

        fibVoucherDetailsPO.setVoucherCode(financialDirectPaymentEntryNotice);
        //创建人,更新人
        fibVoucherDetailsPO.setCreateBy(sysName);
        //预算申请表编号
        fibVoucherDetailsPO.setBgtId(voucherBudgetAllocationVO.getBgtId());
        //金额
        fibVoucherDetailsPO.setVoucherAmount(new BigDecimal(voucherBudgetAllocationVO.getAmountMoneyLowercase()));
        //付款人,收款人编号
        fibVoucherDetailsPO.setVoucherPayUserid(Long.valueOf(voucherBudgetAllocationVO.getPayerId()));
        fibVoucherDetailsPO.setVoucherAccountUserid(Long.valueOf(voucherBudgetAllocationVO.getPayeeId()));
        //状态 未盖章
        fibVoucherDetailsPO.setStatus(0);
        fibVoucherDetailsPO.setCreateTime(new Date());
        fibVoucherDetailsPO.setVersion(1);
        fibVoucherDetailsPO.setDeleted((byte) 1);
        fibVoucherDetailsService.getBaseMapper().insert(fibVoucherDetailsPO);
        is.close();
        return fibVoucherDetailsPO;
    }

    @Override
    public FibVoucherDetailsPO createVoucherBudgetAllocationReceipt(VoucherBudgetAllocationReceiptVO voucherBudgetAllocationReceiptVO) throws Exception {
        //转换填充参数
        VoucherBudgetAllocationReceiptPO voucherBudgetAllocationPO = new VoucherBudgetAllocationReceiptPO();
        BeanUtils.copyProperties(voucherBudgetAllocationReceiptVO, voucherBudgetAllocationPO);
        //对po进行填充
        String timeStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        voucherBudgetAllocationPO.setTime(timeStr);
        //对模板操作
        //读取模板
        FibVoucherPO fibVoucherPO = fibVoucherService.getBaseMapper().selectById(voucherBudgetAllocationReceipt);
        //文件名加后缀
        String voucherName = fibVoucherPO.getVoucherName() + ".pdf";
        //模板在本地的地址
        String voucherPath = fibVoucherPO.getVoucherItems().trim();
        //设置模板地址
        String templateUrl2 = resourcesDir + "/" + voucherPath;
        //设置页数
        Map<Object, String> voAndSheetName2 = new HashMap<>();
        voAndSheetName2.put(voucherBudgetAllocationPO, "Sheet1");
        //填充属性操作
        //将模版和要写入模版的值传入，转换成workbook
        Workbook workbook = ExcelConvertPDF.outPutWorkbookByModel(voAndSheetName2, templateUrl2);
        List<Workbook> workbooks = new ArrayList<>();
        workbooks.add(workbook);
        //设置导出的页面的大小
        RectangleReadOnly pageSize = new RectangleReadOnly(1000.0F, 400.0F);
        //获取转换成pdf的字节流
        byte[] bytes = ExcelConvertPDF.ExcelConvertPDFMultipartFile(workbooks, pageSize);
        //转换成 输入流
        InputStream is = new ByteArrayInputStream(bytes);
        //封装进MockMultipartFile
        MultipartFile multipartFile = new MockMultipartFile(voucherName, voucherName, "application/pdf", is);
        //图片上传
        Result upload = ossFeginService.upload(multipartFile);
        //云端图片地址
        //-------封装voucherDetails记录----------------
        //创建数据库记录
        FibVoucherDetailsPO fibVoucherDetailsPO = new FibVoucherDetailsPO();
        //图片地址
        fibVoucherDetailsPO.setVoucherUrlNotSeal((String) upload.getData().get("url"));

        fibVoucherDetailsPO.setVoucherCode(payRefundNoticeDirectlyCode);
        //创建人,更新人
        fibVoucherDetailsPO.setCreateBy(sysName);
        //预算申请表编号
        fibVoucherDetailsPO.setBgtId(voucherBudgetAllocationReceiptVO.getBgtId());
        //金额
        fibVoucherDetailsPO.setVoucherAmount(new BigDecimal(voucherBudgetAllocationReceiptVO.getAmountMoneyLowercase()));
        //付款人,收款人编号
        fibVoucherDetailsPO.setVoucherPayUserid(Long.valueOf(voucherBudgetAllocationReceiptVO.getPayerId()));
        fibVoucherDetailsPO.setVoucherAccountUserid(Long.valueOf(voucherBudgetAllocationReceiptVO.getPayeeId()));
        //状态 未盖章
        fibVoucherDetailsPO.setStatus(0);
        fibVoucherDetailsPO.setCreateTime(new Date());
        fibVoucherDetailsPO.setVersion(1);
        fibVoucherDetailsPO.setDeleted((byte) 1);
        fibVoucherDetailsService.getBaseMapper().insert(fibVoucherDetailsPO);
        is.close();
        return fibVoucherDetailsPO;
    }

    /**
     * 根据vo生成 直接支付退款通知书
     */
    @Override
    public FibVoucherDetailsPO createAddPayRefundNoticeDirectly(PayRefundNoticeDirectlyVO payRefundNoticeDirectlyVO) throws Exception {

        //转换填充参数
        PayRefundNoticeDirectlyPO addPayRefundNoticeDirectlyPO = new PayRefundNoticeDirectlyPO();
        BeanUtils.copyProperties(payRefundNoticeDirectlyVO, addPayRefundNoticeDirectlyPO);
        //对po进行填充
        String timeStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        addPayRefundNoticeDirectlyPO.setTime(timeStr);
        //对模板操作
        //读取模板
        FibVoucherPO fibVoucherPO = fibVoucherService.getBaseMapper().selectById(payRefundNoticeDirectlyCode);
        //文件名加后缀
        String voucherName = fibVoucherPO.getVoucherName() + ".pdf";
        //模板在本地的地址
        String voucherPath = fibVoucherPO.getVoucherItems().trim();
        //设置模板地址
        String templateUrl2 = resourcesDir + "/" + voucherPath;
        //设置页数
        Map<Object, String> voAndSheetName2 = new HashMap<>();
        voAndSheetName2.put(addPayRefundNoticeDirectlyPO, "Sheet1");
        //填充属性操作
        //将模版和要写入模版的值传入，转换成workbook
        Workbook workbook = ExcelConvertPDF.outPutWorkbookByModel(voAndSheetName2, templateUrl2);
        List<Workbook> workbooks = new ArrayList<>();
        workbooks.add(workbook);
        //设置导出的页面的大小
        RectangleReadOnly pageSize = new RectangleReadOnly(1000.0F, 400.0F);
        //获取转换成pdf的字节流
        byte[] bytes = ExcelConvertPDF.ExcelConvertPDFMultipartFile(workbooks, pageSize);
        //转换成 输入流
        InputStream is = new ByteArrayInputStream(bytes);
        //封装进MockMultipartFile
        MultipartFile multipartFile = new MockMultipartFile(voucherName, voucherName, "application/pdf", is);
        //图片上传
        Result upload = ossFeginService.upload(multipartFile);
        //云端图片地址
        //-------封装voucherDetails记录----------------
        //创建数据库记录
        FibVoucherDetailsPO fibVoucherDetailsPO = new FibVoucherDetailsPO();
        //图片地址
        fibVoucherDetailsPO.setVoucherUrlNotSeal((String) upload.getData().get("url"));

        fibVoucherDetailsPO.setVoucherCode(payRefundNoticeDirectlyCode);
        //创建人,更新人
        fibVoucherDetailsPO.setCreateBy(sysName);
        //预算申请表编号
        fibVoucherDetailsPO.setBgtId(payRefundNoticeDirectlyVO.getBgtId());
        //金额
        System.out.println(payRefundNoticeDirectlyVO.getMoneySmall());
        fibVoucherDetailsPO.setVoucherAmount(new BigDecimal(payRefundNoticeDirectlyVO.getMoneySmall()));
        //付款人,收款人编号
        fibVoucherDetailsPO.setVoucherPayUserid(Long.valueOf(payRefundNoticeDirectlyVO.getRefundId()));
        fibVoucherDetailsPO.setVoucherAccountUserid(Long.valueOf(payRefundNoticeDirectlyVO.getPayeeId()));
        //状态 未盖章
        fibVoucherDetailsPO.setStatus(0);
        fibVoucherDetailsPO.setCreateTime(new Date());
        fibVoucherDetailsPO.setVersion(1);
        fibVoucherDetailsPO.setDeleted((byte) 1);
        fibVoucherDetailsService.getBaseMapper().insert(fibVoucherDetailsPO);
        is.close();
        return fibVoucherDetailsPO;
    }

}
